<template>
  <div>
    <el-card>
      <div class="dingbu">
        <p>
          学生姓名:<el-input v-model="input" placeholder="请输入内容" clearable @input="search" @clear="clear" />
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="manyadd">批量导入</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">新增</el-button>
        </p>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
        />
        <el-table-column
          label="姓名"
          prop="name"
        />
        <el-table-column
          label="班级"
          prop="classbj"
        />
        <el-table-column
          label="籍贯"
          prop="city"
        />
        <el-table-column
          label="阶段"
          prop="level"
        />
        <el-table-column
          label="父母电话"
          prop="parphone"
        />
        <el-table-column
          label="电话"
          prop="phone"
        />
        <el-table-column
          label="市场部"
          prop="shichang"
        />
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="upd(scope.row)"><a class="blue">编辑</a></el-button>
            <el-button @click="del(scope.row)"><a class="red">删除</a></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="t"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog title="新增学生" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="学生姓名"   prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生班级"   prop="classbj">
          <el-input v-model="form.classbj" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生籍贯"   prop="jiguan">
          <el-input v-model="form.city" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生阶段"   prop="level">
          <el-input v-model="form.level" autocomplete="off" />
        </el-form-item>
        <el-form-item label="父母电话"   prop="parphone">
          <el-input v-model="form.parphone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生电话"   prop="phone">
          <el-input v-model="form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生市场部"   prop="shichang">
          <el-input v-model="form.shichang" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addsave">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑用户" :visible.sync="dialogFormVisible2" width="30%">
      <el-form :model="form2" :rules="rules" ref="form2">
        <el-form-item label="学生姓名"   prop="name">
          <el-input v-model="form2.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生班级"   prop="classbj">
          <el-input v-model="form2.classbj" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生籍贯"   prop="jiguan">
          <el-input v-model="form2.city" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生阶段"   prop="level">
          <el-input v-model="form2.level" autocomplete="off" />
        </el-form-item>
        <el-form-item label="父母电话"   prop="parphone">
          <el-input v-model="form2.parphone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生电话"   prop="phone">
          <el-input v-model="form2.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生市场部"   prop="shichang">
          <el-input v-model="form2.shichang" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="updsave">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="批量导入" :visible.sync="dialogFormVisible3" width="30%">
      <el-form >
          <el-upload
          class="upload-demo"
          ref="upload"
          action="http://127.0.0.1:8000/student/student/"
          :on-success="successf"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <!-- <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> -->
          <div slot="tip" class="el-upload__tip">请上传Excel类型文件</div>
          </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="submitUpload">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisible3:false,
      input:'',
      size: 5,
      page: 1,
      t: 0,
      options: [{
        value: '管理员',
        label: '管理员'
      }, {
        value: '普通用户',
        label: '普通用户'
      }],
      form: {
        name: '',
        classbj: '',
        city:'',
        level:'',
        parphone:'',
        phone:'',
        shichang:''
      },
      form2: {
        id: '',
        name: '',
        classbj: '',
        city:'',
        level:'',
        parphone:'',
        phone:'',
        shichang:''
      },
      rules: {
        name: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' }
        ],
        classbj: [
          { required: true, message: '请输入学生班级', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '请输入学生籍贯', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '请输入学生阶段', trigger: 'blur' }
        ],
        parphone: [
          { required: true, message: '请输入父母电话', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入学生电话', trigger: 'blur' }
        ],
        shichang: [
          { required: true, message: '请输入学生市场部', trigger: 'blur' }
        ],
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getstu()
  },
  mounted() {},
  methods: {
    async getstu() {
      var res = await this.http.get('/student/student/',{
        params:{
            page: this.page,
            size: this.size
        }
      })
      console.log(res)
      if (res.data.code == 200) {
        this.tableData = res.data.data
        this.t = res.data.total
      }
    },
    async search() {
      if (!this.pageisornot) {
        this.page = 1
      }
      if (this.input != '') {
        var res = await this.http.get('/student/student/', {
          params: { name: this.input, page: this.page,
            size: this.size }
        })
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.t = res.data.total
        }
      } else {
        this.getstu()
      }
      this.pageisornot = true
    },
    clear() {
      console.log(555555555)
      this.getstu()
    },
    add() {
      this.dialogFormVisible = true
    },
    manyadd(){
      this.dialogFormVisible3 = true
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    successf(){
      this.dialogFormVisible3=false
      this.getstu()
      this.$message.success('批量添加成功')
    },
    async addsave() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          var res = await this.http.post('/student/student/', {
            name: this.form.name,
            classbj: this.form.classbj,
            city:this.form.city,
            level:this.form.level,
            parphone:this.form.parphone,
            phone:this.form.phone,
            shichang:this.form.shichang,
          })
          if (res.data.code == 200) {
            this.dialogFormVisible = false
            this.getstu()
            this.$message.success('添加成功')
          }
        }
      })
    },
    upd(row) {
      this.dialogFormVisible2 = true
      this.form2.id = row.id
      this.form2.name = row.name
      this.form2.classbj = row.classbj
      this.form2.city = row.city
      this.form2.level = row.level
      this.form2.parphone = row.parphone
      this.form2.phone = row.phone
      this.form2.shichang = row.shichang
    },
    async updsave() {
      this.$refs.form2.validate(async(valid) => {
        if (valid) {
          var res = await this.http.put(`/student/student/${this.form2.id}`, {
            name: this.form2.name,
            classbj: this.form2.classbj,
            city:this.form2.city,
            level:this.form2.level,
            parphone:this.form2.parphone,
            phone:this.form2.phone,
            shichang:this.form2.shichang,
          })
          if (res.data.code == 200) {
            this.dialogFormVisible2 = false
            this.getstu()
            this.$message.success('修改成功')
          }
        }
      })
    },
    handleSizeChange(v) {
      this.size = v
      this.getstu()
    },
    handleCurrentChange(v) {
      this.page = v
      this.getstu()
    },
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        var res = await this.http.delete(`/student/student/${row.id}`)
        console.log(res)
        if (res.data.code == 200) {
          this.getstu()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message({
            type: 'error',
            message: '没有权限!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
    .el-input{
      width: 200px;
      margin-left: 7px;
    }
     .dingbu{
      padding-bottom: 15px;
      border-bottom: 2px solid ghostwhite;
    }
    .dingbu p .el-button{
      float: right;
      margin-right: 10px;
    }
    .blue{
      color: #3175fb;
    }
    .red{
      color: red;
    }
    .el-table .el-button{
      background: 0;
      padding-left: 0;
      padding-right: 0;
      display: inline-block;
      border:0;
    }
    .el-pagination{
      margin-top: 20px;
      text-align: center;
    }
</style>
